﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    #region variable
    SqlDataReader dr = null;
    SqlCommand cmd;
    Dataconnect conn = new Dataconnect();

    #endregion
    #region method
    protected bool Select(string user, string email) 
    {
        cmd = new SqlCommand();
        cmd.CommandText = "procSelectUE";
        cmd.Connection = conn.Connect();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@user", user);
        cmd.Parameters.AddWithValue("@email", email);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            _thongbao.Text = "Information sent successfully!!, mail checks to get new password";
            _thongbao.Visible = true;
            return true;
            //Session["userFgPass"] = txt_username.Text;
            //Session["emaiFgPass"] = txt_mail.Text;
        }
        else {
            return false;
        }
    }
    protected void InserttblPgpass(string user, string email) 
    {
        cmd = new SqlCommand();
        cmd.CommandText = "InserTblFgPass";
        cmd.Connection = conn.Connect();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@user", user);
        cmd.Parameters.AddWithValue("@email", email);
        if(cmd.ExecuteNonQuery()>0)
        {
            Response.Write("<script>alert('Insert successfully !!.')</script>");
        }else{
            Response.Write("<script>alert('ERROR !!.')</script>");
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["CustomerName"] != null)
            {
                hlink_logIn.Visible = false;
              
                linkbtn_register.Visible = false;
                name.Text = Session["CustomerName"].ToString();
            }
            else
            {
                hlink_logIn.Visible = true;
                linkbtn_register.Visible = true;
                name.Text = null;
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Select(txt_username.Text, txt_mail.Text) == true)
        {
            InserttblPgpass(txt_username.Text, txt_mail.Text);
            _thongbao.Text = "send successfully !! ";
        }
        else 
        {
            _thongbao.Text = "Username or email not exist ";
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Cart.aspx");
    }

    protected void linkbtn_register_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
}